﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;
namespace OracleDAL.Sec
{
    public class Node
    {
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="NodeISN"></param>
        /// <returns></returns>
        public bool DeleteNode(string NodeISN)
        {
            string sql = "DELETE FROM SEC_NODE WHERE NODEISN = '" + NodeISN + "'";
            int effectRows = DBUtility.OracleHelper.ExecuteSql(sql);
            if (effectRows > 0)
                return true;
            else
                return false;
            throw new NotImplementedException("使用存储过程，检查主键、外键约束");
        }
    
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Sec.Node model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SEC_NODE set ");
			strSql.Append("NODETYPE=:NODETYPE,");
			strSql.Append("NODEINDEX=:NODEINDEX,");
			strSql.Append("ISCATALOG=:ISCATALOG,");
			strSql.Append("LNG=:LNG,");
			strSql.Append("LAT=:LAT,");
			strSql.Append("DISPLAYLEVEL=:DISPLAYLEVEL,");
			strSql.Append("STRIP=:STRIP,");
			strSql.Append("ARMYTYPE=:ARMYTYPE,");
			strSql.Append("ARMYLEAVL=:ARMYLEVEL,");
			strSql.Append("NODEID=:NODEID,");
			strSql.Append("NODENAME=:NODENAME,");
            strSql.Append("CREATETIME=:CREATETIME,");
			strSql.Append("NODELEVEL=:NODELEVEL,");
			strSql.Append("UPNODEID=:UPNODEID,");
			strSql.Append("UPNODENAME=:UPNODENAME,");
			strSql.Append("MAINTAINARMYNAME=:MAINTAINARMYNAME,");
			strSql.Append("ARMYID=:ARMYID");
			strSql.Append(" where NODEISN=:NODEISN ");
			OracleParameter[] parameters = {
					new OracleParameter(":NODEISN", OracleType.VarChar,50),
					new OracleParameter(":NODETYPE", OracleType.VarChar,80),
					new OracleParameter(":NODEINDEX", OracleType.Number,4),
					new OracleParameter(":ISCATALOG", OracleType.Number,4),
					new OracleParameter(":LNG", OracleType.Float,22),
					new OracleParameter(":LAT", OracleType.Float,22),
					new OracleParameter(":DISPLAYLEVEL", OracleType.VarChar,20),
					new OracleParameter(":STRIP", OracleType.VarChar,500),
					new OracleParameter(":ARMYTYPE", OracleType.VarChar,200),
					new OracleParameter(":ARMYLEVEL", OracleType.VarChar,50),
					new OracleParameter(":NODEID", OracleType.VarChar,50),
					new OracleParameter(":NODENAME", OracleType.VarChar,80),
                    new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":NODELEVEL", OracleType.VarChar,20),
					new OracleParameter(":UPNODEID", OracleType.VarChar,50),
					new OracleParameter(":UPNODENAME", OracleType.VarChar,80),
					new OracleParameter(":MAINTAINARMYNAME", OracleType.VarChar,80),
					new OracleParameter(":ARMYID", OracleType.VarChar,50)};
			parameters[0].Value = model.NodeISN;
			parameters[1].Value = model.NodeType;
			parameters[2].Value = model.NodeIndex;
			parameters[3].Value = model.IsCatalog;
			parameters[4].Value = model.LNG;
			parameters[5].Value = model.LAT;
			parameters[6].Value = model.DisplayLevel;
			parameters[7].Value = model.Strip;
			parameters[8].Value = model.ARMYTYPE;
			parameters[9].Value = model.ArmyLevel;
			parameters[10].Value = model.NodeID;
			parameters[11].Value = model.NodeName;
            parameters[12].Value = model.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"); ;
            parameters[13].Value = model.NodeLevel;
            parameters[14].Value = model.UpNodeID;
            parameters[15].Value = model.UpNodeName;
            parameters[16].Value = model.MAINTAINARMYNAME;
            parameters[17].Value = model.ARMYID;

            return OracleHelper.ExecuteSql(strSql.ToString(), parameters) > 0 ? true : false;
		}
     
        public bool NewNode(string NodeISN, string NodeID, string NodeName, DateTime CreateTime, string NodeLevel, string UpNodeID, string UpNodeName, string NodeType, int NodeIndex, int IsCatalog, float LNG, float LAT, string DisplayLevel, string Strip, string ArmyType, string ArmyID, string MAINTAINARMYNAME)
        {
            string sql = "INSERT INTO SEC_NODE(NODEISN,NODEID,NODENAME,CREATETIME,NODELEVEL,UPNODEID,UPNODENAME,NODETYPE,NODEINDEX,ISCATALOG,LNG,LAT,DISPLAYLEVEL,STRIP,ARMYTYPE,ARMYID ,MAINTAINARMYNAME) ";
            sql += "values('" + NodeISN + "','" + NodeID + "','" + NodeName + "',to_date('" + CreateTime.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-MM-dd hh24:mi:ss'),'" + NodeLevel + "','" + UpNodeID + "','" + UpNodeName + "','" + NodeType + "','" + NodeIndex + "'," + IsCatalog + "," + LNG + "," + LAT + ",'" + DisplayLevel + "','" + Strip + "','" + ArmyType + "','" + ArmyID + "','" + MAINTAINARMYNAME + "')";
            int effectRows = DBUtility.OracleHelper.ExecuteSql(sql);
            if (effectRows > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新全部字段用这个
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool NewNode(Model.Sec.Node node)
        {
            return this.NewNode(node.NodeISN, node.NodeID, node.NodeName, node.CreateTime, node.NodeLevel, node.UpNodeID, node.UpNodeName, node.NodeType, node.NodeIndex, node.IsCatalog, node.LNG, node.LAT, node.DisplayLevel, node.Strip, node.ARMYTYPE, node.ARMYID, node.MAINTAINARMYNAME);
        }
        public Model.Sec.Node GetModel(string NodeISN)
        {
            Model.Sec.Node node = new Model.Sec.Node();
            string sql = "SELECT * FROM SEC_NODE WHERE NODEISN='" + NodeISN + "'";
            System.Data.OracleClient.OracleDataReader odr = DBUtility.OracleHelper.ExecuteReader(sql);
            while (odr.Read())
            {
                if (odr["CREATETIME"].ToString() != "")
                    node.CreateTime = Convert.ToDateTime(odr["CREATETIME"]);
                node.DisplayLevel = odr["DISPLAYLEVEL"].ToString();
                node.IsCatalog = Convert.ToInt32(odr["ISCATALOG"]);
                node.LAT = float.Parse(odr["LAT"].ToString());
                node.LNG = float.Parse(odr["LNG"].ToString());
                node.NodeID = odr["NODEID"].ToString();
                node.NodeIndex = Convert.ToInt32(odr["NODEINDEX"]);
                node.NodeISN = odr["NODEISN"].ToString();
                node.NodeLevel = odr["NODELEVEL"].ToString();
                node.NodeName = odr["NODENAME"].ToString();
                node.NodeType = odr["NODETYPE"].ToString();
                node.Strip = odr["STRIP"].ToString();
                node.UpNodeID = odr["UPNODEID"].ToString();
                node.UpNodeName = odr["UPNODENAME"].ToString();
                node.ARMYID = odr["ARMYID"].ToString();
                node.ARMYTYPE = odr["ARMYTYPE"].ToString();
                node.MAINTAINARMYNAME = odr["MAINTAINARMYNAME"].ToString();
                node.ArmyLevel = odr["ARMYLEAVL"].ToString();
            }
            odr.Close();
            return node;
        }
        /// <summary>
        /// 根据条件返回一个model类型
        /// </summary>
        /// <param name="strWhere"> 条件</param>
        /// <returns></returns>
        public Model.Sec.Node GetModelbyWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM sec_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            System.Data.OracleClient.OracleDataReader odr = DBUtility.OracleHelper.ExecuteReader(strSql.ToString());
            Model.Sec.Node node = new Model.Sec.Node();
            while (odr.Read())
            {
                
                if (odr["CREATETIME"].ToString() != "")
                    node.CreateTime = Convert.ToDateTime(odr["CREATETIME"]);
                node.DisplayLevel = odr["DISPLAYLEVEL"].ToString();
                node.IsCatalog = Convert.ToInt32(odr["ISCATALOG"]);
                node.LAT = float.Parse(odr["LAT"].ToString());
                node.LNG = float.Parse(odr["LNG"].ToString());
                node.NodeID = odr["NODEID"].ToString();
                node.NodeIndex = Convert.ToInt32(odr["NODEINDEX"]);
                node.MAINTAINARMYNAME = odr["MAINTAINARMYNAME"].ToString();
                node.ARMYID = odr["ARMYID"].ToString();
                node.ARMYTYPE = odr["ARMYTYPE"].ToString();
                node.NodeISN = odr["NODEISN"].ToString();
                node.NodeLevel = odr["NODELEVEL"].ToString();
                node.NodeName = odr["NODENAME"].ToString();
                node.NodeType = odr["NODETYPE"].ToString();
                node.Strip = odr["STRIP"].ToString();
                node.UpNodeID = odr["UPNODEID"].ToString();
                node.UpNodeName = odr["UPNODENAME"].ToString();
                node.ArmyLevel = odr["ARMYLEAVL"].ToString();
            }
            return node;
        }
        /// <summary>
        /// 根据条件返回一个model List类型
        /// </summary>
        /// <param name="strWhere"> 条件</param>
        /// <param name="flag"> 标记</param>
        /// <returns></returns>
        public List<Model.Sec.Node> GetModelbyWhere(string strWhere,bool flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM sec_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            System.Data.OracleClient.OracleDataReader odr = DBUtility.OracleHelper.ExecuteReader(strSql.ToString());
            List<Model.Sec.Node> modelList = new List<Model.Sec.Node>();
            while (odr.Read())
            {
                Model.Sec.Node msn = new Model.Sec.Node();
                if (odr["CREATETIME"].ToString() != "")
                    msn.CreateTime = Convert.ToDateTime(odr["CREATETIME"]);
                msn.DisplayLevel = odr["DISPLAYLEVEL"].ToString();
                msn.IsCatalog = Convert.ToInt32(odr["ISCATALOG"]);
                msn.LAT = float.Parse(odr["LAT"].ToString());
                msn.LNG = float.Parse(odr["LNG"].ToString());
                msn.NodeID = odr["NODEID"].ToString();
                msn.NodeIndex = Convert.ToInt32(odr["NODEINDEX"]);
                msn.MAINTAINARMYNAME = odr["MAINTAINARMYNAME"].ToString();
                msn.ARMYID = odr["ARMYID"].ToString();
                msn.ARMYTYPE = odr["ARMYTYPE"].ToString();
                msn.NodeISN = odr["NODEISN"].ToString();
                msn.NodeLevel = odr["NODELEVEL"].ToString();
                msn.NodeName = odr["NODENAME"].ToString();
                msn.NodeType = odr["NODETYPE"].ToString();
                msn.Strip = odr["STRIP"].ToString();
                msn.UpNodeID = odr["UPNODEID"].ToString();
                msn.UpNodeName = odr["UPNODENAME"].ToString();
                msn.ArmyLevel = odr["ARMYLEAVL"].ToString();
                modelList.Add(msn);
            }
            return modelList;
        }
        /// <summary>
        /// 根据条件返回记录数
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM sec_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            int count = 0;
           OracleDataReader dr=DBUtility.OracleHelper.ExecuteReader(strSql.ToString());
           while (dr.Read())
               count++;
            return count;
        }
        /// <summary>
        /// 根据条件返回最大的节点ID
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public string GetMaxNodeId(string strWhere)
        {
            
            string sql = "select max(nodeid)  from sec_node where " + strWhere;
            string maxID=string.Empty;
          
            OracleDataReader dr =DBUtility.OracleHelper.ExecuteReader(sql);
            while (dr.Read())
            {
               
                maxID = dr[0].ToString();
            }
            dr.Close();
          
                return maxID;
           
           
        }
        /// <summary>
        /// 删除节点时，移动相邻节点专用
        /// </summary>
        /// <returns></returns>
        public bool DelAndMoveNode(int i,string nodeid)
        {
           string  sql = "update sec_node set  nodeindex=" + i + " where nodeindex=" + i + 1 + " and nodeid like '" + nodeid + "__'";
             int rows=DBUtility.OracleHelper.ExecuteSql(sql);
             if (rows > 0)
                 return true;
             else
                 return false;
        }
        public DataTable getTable(string strWhere)
        {
            string sql = "select * from sec_node where " + strWhere;
            return OracleHelper.Query(sql).Tables[0];
        }
        /// <summary>
        /// 从sec_unit和sec_node表里根据单位的unitISN得到Node的NodeISN,NodeID,NodeName,UpnodeID,NodeLevel
        /// </summary>
        /// <param name="unitISN"></param>
        /// <returns></returns>
        public DataTable getDataTableBYUnitISN(string unitISN)
        {
            string sql = "select n.nodeisn,n.nodeid,n.nodename,n.upnodeid,n.nodelevel from sec_unit u,sec_node n where u.upnodeid=n.nodeid and unitisn='" + unitISN + "'";
            return  OracleHelper.Query(sql).Tables[0];
        }
    }
}
